using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SBPweb.Contracts;
using SBPweb.Contracts.Options;
using SBPweb.Contracts.Implementation;

using SBPweb.Modules.Modularity;

namespace SBPweb.Modules.ChatController.Controls
{
	public partial class ChatOptionsControl : UserControl
	{
		#region Private properties

		private IOptionsService mOptionsService;
		private ChatConfiguration mChatConfiguration;
		private IDictionary mSmileys;
		private IModule mParentModule;

		#endregion

		#region Constructor logic

		public ChatOptionsControl(IModule parent, IOptionsService optionsService)
		{
			mParentModule = parent;
			mChatConfiguration = new ChatConfiguration(parent);
			mOptionsService = optionsService;
			InitializeComponent();
		}

		#endregion

		#region Public properties

		public IDictionary Smileys
		{
			get
			{
				return mSmileys;
			}
			set
			{
				mSmileys = value;

				lbSmileys.BeginUpdate();
				lbSmileys.Items.Clear();
				if (value != null)
				{
					try
					{
						foreach (string key in value.Keys)
						{
                            if (!key.ToString().StartsWith("[") && !key.ToString().EndsWith("["))
                            {
                                lbSmileys.Items.Add(key);
                                if (lbSmileys.ItemHeight < (value[key] as Image).Height)
                                {
                                    lbSmileys.ItemHeight = (value[key] as Image).Height;
                                }
                            }
						}
					}
					catch (Exception ex)
					{
						if (mParentModule != null)
						{
							mParentModule.Host.LogManager.WriteInfo(this, "Cannot render smileys. " + ex.Message, "ChatController");
						}
					}
				}
				lbSmileys.EndUpdate();
			}
		}

        public ChatConfiguration ChatConfiguration
        {
            get
            {
                return mChatConfiguration;
            }
        }

		#endregion

		#region Persistency

		internal void SavePersistence(PersistencyDictionary state)
		{
			mChatConfiguration.SavePersistence(state);
		}

		internal void LoadPersistence(PersistencyDictionary state)
		{
			mChatConfiguration = new ChatConfiguration(mParentModule, state);

			lblOutgoing.Font = mChatConfiguration.OutGoingMessageFont;
			lblOutgoing.ForeColor = mChatConfiguration.OutGoingMessageColor;

			lblIncoming.Font = mChatConfiguration.IncomingMessageFont;
			lblIncoming.ForeColor = mChatConfiguration.InComingMessageColor;

			lblOther.Font = mChatConfiguration.OtherTextFont;
			lblOther.ForeColor = mChatConfiguration.OtherTextColor;

			lblConfOwner.Font = mChatConfiguration.OwnerMessageFont;
			lblConfOwner.ForeColor = mChatConfiguration.OwnerMessageColor;

			lblConfOwnerOther.Font = mChatConfiguration.OwnerOtherTextFont;
			lblConfOwnerOther.ForeColor = mChatConfiguration.OwnerOtherTextColor;

			lblChatHistory.Font = mChatConfiguration.ChatHistoryTextFont;
			lblChatHistory.ForeColor = mChatConfiguration.ChatHistoryTextColor;

			lblHistoryHeader.Font = mChatConfiguration.ChatHistoryOtherTextFont;
			lblHistoryHeader.ForeColor = mChatConfiguration.ChatHistoryOtherTextColor;

			cbxPlayBackgroundMessages.Checked = mChatConfiguration.PlayBackgroundSound;

			cbxShowOfflinesForConference.Checked = mChatConfiguration.ShowOfflinesForConference;

			cbxHistory.Checked = mChatConfiguration.ShowHistory;

			nudHistroyLines.Value = mChatConfiguration.HistoryLines;
		}

		#endregion

		#region UI event handlers

		private void btnFont_Click(object sender, EventArgs e)
		{
			if ((sender as SBPweb.Controls.Windows.Common.Button) == null)
			{
				return;
			}
			try
			{
				Font originalFont = null;
				switch ((sender as SBPweb.Controls.Windows.Common.Button).Name)
				{
					case "btnFontOutgoing":
						originalFont = lblOutgoing.Font;
						break;
					case "btnFontIncoming":
						originalFont = lblIncoming.Font;
						break;
					case "btnFontOther":
						originalFont = lblOther.Font;
						break;
					case "btnFontConfOwner":
						originalFont = lblConfOwner.Font;
						break;
					case "btnFontConfOwnerOther":
						originalFont = lblConfOwnerOther.Font;
						break;
					case "btnFontHistoryHeader":
						originalFont = lblHistoryHeader.Font;
						break;
					case "btnFontHistory":
						originalFont = lblChatHistory.Font;
						break;
					default:
						return;
				}
				fontDialog.Font = originalFont;
				fontDialog.ScriptsOnly = false;
				fontDialog.MaxSize = 40;
				fontDialog.MinSize = 8;
				if (fontDialog.ShowDialog() == DialogResult.OK)
				{
					switch ((sender as SBPweb.Controls.Windows.Common.Button).Name)
					{
						case "btnFontOutgoing":
							lblOutgoing.Font = fontDialog.Font;
							mChatConfiguration.OutGoingMessageFont = lblOutgoing.Font;
							break;
						case "btnFontIncoming":
							lblIncoming.Font = fontDialog.Font;
							mChatConfiguration.IncomingMessageFont = lblIncoming.Font;
							break;
						case "btnFontOther":
							lblOther.Font = fontDialog.Font;
							mChatConfiguration.OtherTextFont = lblOther.Font;
							break;
						case "btnFontConfOwner":
							lblConfOwner.Font = fontDialog.Font;
							mChatConfiguration.OwnerMessageFont = lblConfOwner.Font;
							break;
						case "btnFontConfOwnerOther":
						    lblConfOwnerOther.Font = fontDialog.Font;
							mChatConfiguration.OwnerOtherTextFont = lblConfOwnerOther.Font;
						    break;
						case "btnFontHistory":
							lblChatHistory.Font = fontDialog.Font;
							mChatConfiguration.ChatHistoryTextFont = lblChatHistory.Font;
							break;
						case "btnFontHistoryHeader":
							lblHistoryHeader.Font = fontDialog.Font;
							mChatConfiguration.ChatHistoryOtherTextFont = lblHistoryHeader.Font;
							break;
					}
					mOptionsService.IsModified = true;
				}
			}
			catch (ArgumentException exc)
			{
				MessageBox.Show(exc.Message, "Font Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
			}
		}

		private void btnColor_Click(object sender, EventArgs e)
		{
			if ((sender as SBPweb.Controls.Windows.Common.Button) == null)
			{
				return;
			}

			switch ((sender as SBPweb.Controls.Windows.Common.Button).Name)
			{
				case "btnColorOutgoing":
					colorDialog.Color = mChatConfiguration.OutGoingMessageColor;
					break;
				case "btnColorIncoming":
					colorDialog.Color = mChatConfiguration.InComingMessageColor;
					break;
				case "btnColorOther":
					colorDialog.Color = mChatConfiguration.OtherTextColor;
					break;
				case "btnColorConfOwner":
					colorDialog.Color = mChatConfiguration.OwnerMessageColor;
					break;
				case "btnColorConfOwnerOther":
					colorDialog.Color = mChatConfiguration.OwnerOtherTextColor;
					break;
				case "btnColorHistory":
					colorDialog.Color = mChatConfiguration.ChatHistoryTextColor;
					break;
				case "btnColorHistoryHeader":
					colorDialog.Color = mChatConfiguration.ChatHistoryOtherTextColor;
					break;
			}

			if (colorDialog.ShowDialog() == DialogResult.OK)
			{
				switch ((sender as SBPweb.Controls.Windows.Common.Button).Name)
				{
					case "btnColorOutgoing":
						lblOutgoing.ForeColor = colorDialog.Color;
						mChatConfiguration.OutGoingMessageColor = colorDialog.Color;
						break;
					case "btnColorIncoming":
						lblIncoming.ForeColor = colorDialog.Color;
						mChatConfiguration.InComingMessageColor = colorDialog.Color;
						break;
					case "btnColorOther":
						lblOther.ForeColor = colorDialog.Color;
						mChatConfiguration.OtherTextColor = colorDialog.Color;
						break;
					case "btnColorConfOwner":
						lblConfOwner.ForeColor = colorDialog.Color;
						mChatConfiguration.OwnerMessageColor = colorDialog.Color;
						break;
					case "btnColorConfOwnerOther":
						lblConfOwnerOther.ForeColor = colorDialog.Color;
						mChatConfiguration.OwnerOtherTextColor = colorDialog.Color;
						break;
					case "btnColorHistory":
						lblChatHistory.ForeColor = colorDialog.Color;
						mChatConfiguration.ChatHistoryTextColor = colorDialog.Color;
						break;
					case "btnColorHistoryHeader":
						lblHistoryHeader.ForeColor = colorDialog.Color;
						mChatConfiguration.ChatHistoryOtherTextColor = colorDialog.Color;
						break;
				}

				mOptionsService.IsModified = true;
			}
		}

		private void cbxHistory_CheckedChanged(object sender, EventArgs e)
		{
			mChatConfiguration.ShowHistory = cbxHistory.Checked;
			mOptionsService.IsModified = true;
			nudHistroyLines.Enabled = cbxHistory.Checked;
		}

		private void cbxPlayBackgroundMessages_CheckedChanged(object sender, EventArgs e)
		{
			mChatConfiguration.PlayBackgroundSound = cbxPlayBackgroundMessages.Checked;
			mOptionsService.IsModified = true;
		}

		private void cbxShowOfflinesForConference_CheckedChanged(object sender, EventArgs e)
		{
			mChatConfiguration.ShowOfflinesForConference = cbxShowOfflinesForConference.Checked;
			mOptionsService.IsModified = true;
		}

		private void nudHistroyLines_ValueChanged(object sender, EventArgs e)
		{
			mChatConfiguration.HistoryLines = (int)(nudHistroyLines.Value);
			mOptionsService.IsModified = true;
		}

		#endregion

		#region Event handlers

		private void lbSmileys_DrawItem(object sender, DrawItemEventArgs e)
		{
			if (e.Index < 0)
			{
				return;
			}
			try
			{
				string text = lbSmileys.Items[e.Index].ToString();
				SizeF textSize = e.Graphics.MeasureString(text, e.Font);
				int textWidth = (int)(textSize.Width);
				int textHeight = (int)(textSize.Height);
				int textPadding = (e.Bounds.Height - textHeight) / 2;
				Image image = (mSmileys[text] as Image);
				int imageHeight = Math.Min(image.Height, e.Bounds.Height * 2 / 3);
				int imagePadding = (e.Bounds.Height - imageHeight) / 2;

				e.Graphics.DrawString(text, e.Font, Brushes.Black, e.Bounds.X, e.Bounds.Y + textPadding);
				e.Graphics.DrawImage(image, e.Bounds.X + textWidth, e.Bounds.Y + imagePadding, image.Width, image.Height);
			}
			catch (Exception ex)
			{
				if (mParentModule != null)
				{
					mParentModule.Host.LogManager.WriteInfo(this, "Cannot rendes smileys. " + ex.Message, "ChatController");
				}
			}
		}

		#endregion
	}
}
